package com.linln.modules.message.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linln.common.constant.NoticeTypeConst;
import com.linln.common.constant.ShenheStatusConst;
import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import com.linln.modules.member.domain.Membernotice;
import com.linln.modules.member.service.MembernoticeService;
import com.linln.modules.member.service.NoticeService;
import com.linln.modules.message.domain.MessageInfo;
import com.linln.modules.message.repository.MessageInfoRepository;
import com.linln.modules.message.service.MessageInfoService;

/**
 * @author hct
 * @date 2019/09/12
 */
@Service
public class MessageInfoServiceImpl implements MessageInfoService {

    @Autowired
    private MessageInfoRepository messageInfoRepository;
    @Autowired
    private MembernoticeService membernoticeService;
    @Autowired
    private NoticeService noticeService;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public MessageInfo getById(Long id) {
        return messageInfoRepository.findById(id).orElse(null);
    }

    
         /**
     * 根据ID查询数据
     * @param orderId 主键ID
     */
    public  MessageInfo getByOrderId(String orderId) {
        return messageInfoRepository.findByOrderId(orderId);

    }

    public Integer updatePayStatus( String orderId) {
        return messageInfoRepository.updatePayStatus(orderId);

    }
    /**
     * 退款 修改状态
     * @param orderId
     * @return
     */
    public Integer updateRefundStatus( String orderId) {
        return messageInfoRepository.updateRefundStatus(orderId);

    }
    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<MessageInfo> getPageList(Example<MessageInfo> example) {
        // 创建分页对象
         PageRequest page = PageSort.pageRequest();
         return messageInfoRepository.findAll(example, page);
    }

    @Override
    public Page<MessageInfo> getPageList(Example<MessageInfo> example,Integer pageIndex,Integer pageSize) {
        // 创建分页对象
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        if(pageIndex==null) {
        	pageIndex=1;
        }
        
    	PageRequest page = PageSort.pageRequest(pageIndex,pageSize,sort);
         return messageInfoRepository.findAll(example, page);
    }
    @Override
    public Page<MessageInfo> getPageList(Example<MessageInfo> example,Integer pageIndex,Sort sort) {
        // 创建分页对象
         PageRequest page = PageSort.pageRequest(pageIndex,sort);
         return messageInfoRepository.findAll(example, page);
    }
    
    @Override
    public Page<MessageInfo> getPageList(Example<MessageInfo> example,Integer pageIndex,Integer pageSize,Sort sort){
    	PageRequest page = PageSort.pageRequest(pageIndex,pageSize,sort);
        return messageInfoRepository.findAll(example, page);
    }

     /**
     * 保存数据
     * @param messageInfo 实体对象
     */
    @Override
    public MessageInfo save(MessageInfo messageInfo) {
        return messageInfoRepository.save(messageInfo);
    }
    @Override
    @Transactional
    public MessageInfo saveAndSendNotice(MessageInfo messageInfo) {
    	MessageInfo messageInfonew=messageInfoRepository.save(messageInfo);
    	/*if(messageInfonew!=null && messageInfonew.getId()>0) {
    		membernoticeService.updateIsread(1,messageInfonew.getId());
    		//发送消息
     		//ok发送消息
    		Notice notice=new Notice();
    		notice.setArea(messageInfo.getArea());
    		notice.setContent("一条待审核信息");
            notice.setCreateDate(new Date());
            notice.setMember(messageInfo.getMember());
            notice.setType(NoticeTypeConst.MESSAGE);
            notice.setTypeId(messageInfonew.getId());
            notice.setIsread(0); 
    		noticeService.save(notice);
     	}*/
    	return messageInfonew;
    }


    @Override
    @Transactional
    public Boolean downMessageStatus(Integer messageStatus,Long id){
        if(messageInfoRepository.downMessageStatus(id)>0) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    @Transactional
    public Boolean updateMessageStatus(Integer messageStatus,Long id) {
    	MessageInfo messageInfo=messageInfoRepository.getOne(id);
		
    	if(messageInfoRepository.updateMessageStatus(messageStatus,id)>0) {
    		//ok发送消息
    		/*Membernotice membernotice=new Membernotice();
    		membernotice.setArea(messageInfo.getArea());   		
    		if(messageStatus==ShenheStatusConst.OK) {
    			membernotice.setContent("审核信息通过");
        		
    		}else if(messageStatus==ShenheStatusConst.REFUSE) {
    			membernotice.setContent("审核信息拒绝");

    		}
    		membernotice.setIsread(0); 
    		membernotice.setCreateDate(new Date());
    		membernotice.setMember(messageInfo.getMember());
    		membernotice.setNoticestatus(messageStatus);
    		membernotice.setType(NoticeTypeConst.MESSAGE);
    		membernotice.setTypeid(id); 
    		membernoticeService.save(membernotice);*/
    		return true;
    	}else {
    		return false;
    	}
    }

    @Override
    public Boolean downMessageStatus(Long id) {
        return messageInfoRepository.downMessageStatus(id)>0;
    }

    /**
            * 根据id查询所有信息
     * @param ids
     * @return
     */
    @Override
    public List<MessageInfo> getList(List<Long> ids) {
        // 创建分页对象
          return messageInfoRepository.findAllById(ids);
    }
    /**
     * 信息的阅读数增加1
     */
    @Transactional
    public Boolean updateIsReadAddById(Long id) {
    	return messageInfoRepository.updateIsReadAddById(id) > 0;
    }
   public  Boolean updateIsCollectAddById(Long id) {
    	return messageInfoRepository.updateIsCollectAddById(id) > 0;
    }

	
	public  Boolean updateIsCollectSubById(Long id) {
		return messageInfoRepository.updateIsCollectSubById(id) > 0;
	 }
	
   public  Boolean updateIsZanAddById(Long id) {
	    	return messageInfoRepository.updateIsZanAddById(id) > 0;
	}

		
	public  Boolean updateIsZanSubById(Long id) {
			return messageInfoRepository.updateIsZanSubById(id) > 0;
	}
	public Integer getCountByArea(String area) {
	    	return messageInfoRepository.getCountByArea(area);
	}
	
	public Integer getCountByAreaAndToday(String area) {
    	return messageInfoRepository.getCountByAreaAndToday(area);
}
	public long  getAllCount(Example<MessageInfo> example) {
		return messageInfoRepository.count(example);
	}

	/**
	 * 当前时效里面展示的置顶消息
	 */
	public List<MessageInfo> queryListByArea(String area,Long type){
		if(type==null || type == 0 ) {
		return messageInfoRepository.queryListByArea(area);
		}else {
			return messageInfoRepository.queryListByArea(area,type);

		}
	}

	/**
	 * 每天展示置顶的消息
	 * @param area
	 * @return
	 */
	public List<MessageInfo> queryTopDayListByArea(String area,Long type){
		if(type==null || type == 0) {
			
			return messageInfoRepository.queryTopDayListByArea(area);
			
		}else {
			
			return messageInfoRepository.queryTopDayListByArea(area,type);

		}

	}

	 /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
	@Override
	public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
		return messageInfoRepository.updateStatus(statusEnum.getCode(), idList) > 0;
	}
	
	@Override
	public long  getDayAddCount(Long userId) {
		return messageInfoRepository.getDayAddCount(userId);

	}
	
	
	

}